import pino from 'pino';
import {isDocker} from "./tools";

const translateTimeToCST = () => {
    const date = new Date();
    const options = {timeZone: 'Asia/Shanghai', hour12: false};
    const value = date.toLocaleString('zh-CN', options).replace(/\//g, '-').split(' ');
    return value[0] + '--' + value[1];
}
export let console_level: string = 'info';
if (isDocker()) console_level = 'error';
const logger = pino({
    level: 'info',
    timestamp: () => `,"time":"${translateTimeToCST()}"`,
    transport: {
        targets: [
            {
                target: 'pino-pretty',
                options: {
                    colorize: true,
                },
                level: console_level,
            },
            {
                target: 'pino/file',
                options: {
                    destination: './logs/info.log',
                    colorize: true,
                },
                level: 'info',
            },
            {
                target: 'pino/file',
                options: {
                    destination: './logs/error.log',
                    colorize: true,
                },
                level: 'error',
            }
        ],
    },
    formatters: {
        bindings: (bindings) => {
            const {pid, hostname, ...rest} = bindings
            return rest
        }
    },
});

export default logger;